package com.suke.czx.modules.team.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.suke.czx.common.utils.FieldConstant;
import com.suke.czx.common.utils.R;
import com.suke.czx.modules.sys.entity.SysUser;
import com.suke.czx.modules.team.entity.Team;
import com.suke.czx.modules.team.entity.TeamSearchBo;
import com.suke.czx.modules.team.mapper.TeamMapper;
import com.suke.czx.modules.team.service.TeamService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 应用模块名称: 团队
 * 代码描述: 团队业务层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/08 2:07
 */
@Service
@AllArgsConstructor
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {

    private final TeamMapper teamMapper;

    /**
     * 分页查询团队成员
     * @param teamSearchBo 查询BO
     * @return VO
     */
    @Override
    public R pageTeamList(TeamSearchBo teamSearchBo) {
        Page<Team> page = PageHelper.startPage(teamSearchBo.getPage(), teamSearchBo.getLimit(), true,true,true);
        // 获取信息查询结果集
        List<Team> teamList = teamMapper.pageTeamList(teamSearchBo);
        return Objects.requireNonNull(R.ok().put(FieldConstant.TOTAL, page.getTotal())).setData(Optional.ofNullable(teamList).orElse(Lists.newArrayList()));
    }

    /**
     * 删除团队
     * @param teamId 团队编码
     * @param userId 员工编码
     * @param operatorId 操作者编码
     * @return VO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDelTeam(Long teamId, Long userId, Long operatorId) {
        teamMapper.updateDelTeam(teamId, userId, operatorId);
        return R.ok();
    }

    /**
     * 根据项目查询团队
     * @param projectId 项目编码
     * @return 团队成员数据集
     */
    @Override
    public List<SysUser> searchTeamByProjectId(Long projectId) {
        return teamMapper.searchTeamByProjectId(projectId);
    }

}
